package progressCaseBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;

/**
 * Created by yangxianming on 2021/2/3.
 * x
 */
public class ShowProgressCase implements TestServerBusinessImpl {

    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig){
        ReturnJson returnJson = new ReturnJson();
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        JSONArray result = new JSONArray();
        String action = request.getParameter("action");

        if (Objects.equals("showCase", action)){
            //列出相关case
            String progressName = request.getParameter("progressName");
            String sqlProgressCase = "SELECT * FROM progresscase WHERE progressname = '"+progressName+"' AND state != '3'";
            SqlReturnModel srm = sqlDispatch.selectWord(sqlProgressCase);

            //查出来的数据按照模块分类
            /*
            var caseListData = [
        {"moduleName":"福员外首页","caseList":[["可用范围选择条目，当已经选择了\"自定义区域\" 或 \"自定义品牌门店\"，再切换选择的时候会清楚之前的选择，并给出提示","condition1","steps1","1","1"],["可用范围区域限制后管信息输入验证：可以按照省市区选择限制范围，可以按照自定义范围选择限制范围","condition2","steps2","2","2"],["菜单布局\"宫格型\"，编辑菜单验证，验证内容：新增菜单，删除菜单，icon图片上传，各输入框文字输入","condition3","steps3","3","3"]]},
        {"moduleName":"福员外首页2","caseList":[["title4","condition4","steps2","1","4"],["title5","condition5","steps5","2","5"]]},
        {"moduleName":"福员外首页3","caseList":[["title6","condition6","steps6","1","6"]]}
    ];
            */
            ArrayList<String> index = new ArrayList<String>();//用于索引下面的hashmap
            HashMap<String, JSONArray> caseArr = new HashMap<String, JSONArray>();
            ArrayList<ArrayList<String>> l = srm.getSelectReturnData();
            for (ArrayList<String> one: l){
                String module = one.get(3);
                JSONArray oneArr = new JSONArray();
                oneArr.add(one.get(4));
                oneArr.add(one.get(5));
                oneArr.add(one.get(6));
                oneArr.add(one.get(7));
                oneArr.add(one.get(1));
                if (caseArr.containsKey(module)){
                    caseArr.get(module).add(oneArr);
                }else {
                    caseArr.put(module, new JSONArray());
                    caseArr.get(module).add(oneArr);
                    index.add(module);
                }
            }

            for (String module: index){
                JSONObject one = new JSONObject();
                one.put("moduleName", module);
                one.put("caseList", caseArr.get(module));
                result.add(one);
            }

            returnJson.setResult(result);
            returnJson.setApiStatus((srm.getIsSuccess()) ? 1 : 0);
            returnJson.setErrorMsg(srm.getFailingReason());
        }




        return returnJson.toString();
    }

}
